package ip

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
)

type data struct {
	Ip       string
	Province string
	City     string
	Isp      string
	Desc     string
}

type toolbon struct {
	Code int
	Msg  string
	Data struct {
		Ip         string
		Province   string
		ProvinceId int
		City       string
		CityId     int
		Isp        string
		Desc       string
	}
}

type Ip struct {
}

func (i *Ip) Check(ip string) data {
	return i.toolbon(ip)
}

//	toolbon ,https://www.toolbon.com/open的开放接口
//		参数
//			ip ip地址
func (i *Ip) toolbon(ip string) data {
	client := &http.Client{}
	req, _ := http.NewRequest("GET", "https://server.toolbon.com/api/ip?ip="+ip+"&token=efb4d3ba698e46e281d36ae670ad0dc9", nil)
	resp, _ := client.Do(req)
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	var bodyUn toolbon
	json.Unmarshal(body, &bodyUn)
	return data{
		Ip:       ip,
		Province: bodyUn.Data.Province,
		City:     bodyUn.Data.City,
		Isp:      bodyUn.Data.Isp,
		Desc:     bodyUn.Data.Desc,
	}
}
